package com.chdwmp.product.controller;

import com.chdwmp.product.bean.CommonResponse;
import com.chdwmp.product.bean.PageResult;
import com.chdwmp.product.bean.User;
import com.chdwmp.product.bean.UserListSearch;
import com.chdwmp.product.po.UserPo;
import com.chdwmp.product.service.UserService;
import com.chdwmp.product.vo.LoginUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: LLL
 * @Date: 2021/3/6 21:04
 * @Description: 用户模块接口层
 */
@Api(description = "用户模块", tags = {"用户模块接口控制层"})
@RequestMapping("/users")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登录", httpMethod = "POST", response = CommonResponse.class)
    public CommonResponse login(@RequestBody LoginUserVO loginUserVO) throws Exception {
        UserPo po = userService.login(loginUserVO);
        return new CommonResponse(po);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "登出", httpMethod = "POST", response = CommonResponse.class)
    public CommonResponse logout(@RequestHeader("token") String token) {
        userService.logout(token);
        return new CommonResponse();
    }

    @PostMapping
    @ApiOperation(value = "添加账号", httpMethod = "POST", response = CommonResponse.class)
    public CommonResponse addUser(@RequestBody User user) throws Exception {
        userService.addUser(user);
        return new CommonResponse();
    }

    @PostMapping("/updateUser")
    @ApiOperation(value = "修改账号", httpMethod = "POST", response = CommonResponse.class)
    public CommonResponse updateUser(@RequestBody User user) throws Exception {
        userService.updateUser(user);
        return new CommonResponse();
    }

    @PostMapping("/deleteUser/{id}")
    @ApiOperation(value = "删除账号", httpMethod = "POST", response = CommonResponse.class)
    public CommonResponse deleteUser(@PathVariable String id) throws Exception {
        userService.deleteUser(id);
        return new CommonResponse();
    }

    @PostMapping("/getUserList")
    @ApiOperation(value = "获取用户列表", httpMethod = "POST", response = CommonResponse.class)
    public CommonResponse getUserList(@RequestBody UserListSearch search) throws Exception {
        PageResult result = userService.getUserList(search);
        return new CommonResponse(result);
    }


}
